#include "AASquare.h"
#include "Ray.h"
#include "Console.h"

AASquare::AASquare()
{
}

AASquare::~AASquare()
{
}

void AASquare::renderGL()
{
	Vector3 v0 = m_center - m_width*Vector3(.5);
	Vector3 v1 = m_center + m_width*Vector3(.5);

	glBegin(GL_QUADS);
        glVertex3f(v0.x, m_center.y, v0.z);
        glVertex3f(v1.x, m_center.y, v0.z);
        glVertex3f(v1.x, m_center.y, v1.z);
		glVertex3f(v0.x, m_center.y, v1.z);
    glEnd();
}

void AASquare::preCalc()
{
	m_min = Vector3(m_center - m_width*Vector3(.5));
	m_max = Vector3(m_center + m_width*Vector3(.5));
}

bool AASquare::intersect(HitInfo& result, const Ray& ray,
                  float tMin, float tMax)
{
	float t = (m_center.y - ray.o.y)*ray.inv_d.y;
	Vector3 p = ray.o + t*ray.d;

	if(t > tMin && t < tMax && (p.x > m_min.x) && (p.z > m_min.z) && (p.x < m_max.x) && (p.z < m_max.z)) {
		result.t = t;
		result.P = p;
		if(dot(ray.d, Vector3(0,1,0))>0)
			result.N = Vector3(0,-1,0);
		else
			result.N = Vector3(0,1,0);
		result.material = m_material;
		return true;
	}

	return false;
}
